package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏列表：geofenceQuery{}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        /* 遍历取出每一个围栏V0，为围栏补全它的车辆数据 */
        for(int i=0;i<list.size();i++){
            GeofenceVO geofenceVO = list.get(i);
            //封装车辆的查询对象
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList=vehicleMapper.selectVehicle(vehicleQuery);
            //定义变量用来保存当前围栏上绑定的车辆总数
            int totalNum = vehicleVOList.size();
            //定义变量用来保存当前围栏上的可用车辆数
            int availableNum = 0;
            for(VehicleVO vehicleVO:vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){//如果当前遍历到的车辆状态为空闲
                    availableNum++;//可用车辆数+1
                }
            }
            //给围栏V0 补全数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("新增电子围栏：geofenceSaveParam{}", geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam, geofence);
        geofence.setStatus("1");
        geofence.setCreateTime(new Date());
        geofenceMapper.insert(geofence);
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改状态：id{},status{}", id, status);
        Geofence geofence=new Geofence();
        geofence.setId(id);
        geofence.setStatus(status);
        geofence.setUpdateTime(new Date());
        geofenceMapper.update(geofence);
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏：{}", id);
        //1.封装车辆的查询条件
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用查车的方法
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果该围栏下有绑定的车辆,无法直接测除
        if(list.size()>0 && list != null){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else {
            //4.如果该围栏上没有绑定车辆，直接测除!
            geofenceMapper.deleteGeofence(id);
        }

    }

}
